<?php

class IndexController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        
    }

    public function nativesAction() {
        $this->_redirect('/departament/index/model/natives');
    }

    public function odbcAction() {
        $this->_redirect('/departament/index/model/odbc');
    }

    public function adodbAction() {
        $this->_redirect('/departament/index/model/adodb');
    }

    public function pdoAction() {
        $this->_redirect('/departament/index/model/pdo');
    }

    public function mysqliAction() {
        $this->_redirect('/departament/index/model/mysqli');
    }

    public function ajaxAction() {
        $this->view->title = "Ajax";
        $this->view->headTitle($this->view->title, 'PREPEND');
        $request = $this->getRequest();
        $nModel = 'pdo';
        $model = Application_Model_NouModel::crearModel($nModel);
        if ($model != null) {
            $this->view->llistat = $model->listEmpsOrd('dni', 'asc');
        }
    }

    public function llistatAction() {

        $request = $this->getRequest();
        $ordre = $request->getParam('ordre');
        $camp = $request->getParam('camp');
        $model = Application_Model_NouModel::crearModel('pdo');
        $llistat = $model->listEmpsOrd($camp, $ordre);
        $total = count($llistat);
        if ($total > 0) {
            echo "<table class='table table-striped'>";
            echo "<thead><tr><th>#</th>";
            if ($camp == 'dni') {
                if ($ordre == 'asc') {
                    echo "<th><span onclick=\"OrdenarPer('dni','desc')\">DNI<i class='icon-chevron-up'></i></span></th>";
                } else {
                    echo "<th><span onclick=\"OrdenarPer('dni','asc')\">DNI<i class='icon-chevron-down'></i></span></th>";
                }
            } else {
                echo "<th><span onclick=\"OrdenarPer('dni','asc')\">DNI</span></th>";
            }
            if ($camp == 'nom') {
                if ($ordre == 'asc') {
                    echo "<th><span onclick=\"OrdenarPer('nom','desc')\">Nom<i class='icon-chevron-up'></i></span></th>";
                } else {
                    echo "<th><span onclick=\"OrdenarPer('nom','asc')\">Nom<i class='icon-chevron-down'></i></span></th>";
                }
            } else {
                echo "<th><span onclick=\"OrdenarPer('nom','asc')\">Nom</span></th>";
            }
            if ($camp == 'cognom') {
                if ($ordre == 'asc') {
                    echo "<th><span onclick=\"OrdenarPer('cognom','desc')\">Cognom<i class='icon-chevron-up'></i></span></th>";
                } else {
                    echo "<th><span onclick=\"OrdenarPer('cognom','asc')\">Cognom<i class='icon-chevron-down'></i></span></th>";
                }
            } else {
                echo "<th><span onclick=\"OrdenarPer('cognom','asc')\">Cognom</span></th>";
            }
            if ($camp == 'departament') {
                if ($ordre == 'asc') {
                    echo "<th><span onclick=\"OrdenarPer('departament','desc')\">Departament<i class='icon-chevron-up'></i></span></th>";
                } else {
                    echo "<th><span onclick=\"OrdenarPer('departament','asc')\">Departament<i class='icon-chevron-down'></i></span></th>";
                }
            } else {
                echo "<th><span onclick=\"OrdenarPer('departament','asc')\">Departament</span></th>";
            }
            for ($i = 0; $i < $total; $i++) {
                echo "<tr>";
                echo ("<td>" . ($i + 1) . "</td><td>" . $llistat[$i]['dni'] . "</td>");
                echo ("<td>" . $llistat[$i]['nom'] . "</td>");
                echo ("<td>" . $llistat[$i]['cognom'] . "</td>");
                echo ("<td>" . $llistat[$i]['departament'] . "</td>");
                echo "<td><a href=/Practica/public/index.php/empleat/editar/ajax/true/model/pdo/id/" . $llistat[$i]['id'] . " class='btn btn-mini'><i class=\"icon-pencil\"></i>Editar</a></td>";
                echo "<td><a href=/Practica/public/index.php/empleat/eliminar/ajax/true/model/pdo/id/" . $llistat[$i]['id'] . " class='btn btn-danger btn-mini'><i class=\"icon-remove\"></i>Eliminar</a></td>";
                echo "</tr>";
            }
            echo "</table>";
        } else {
            echo "<p style='margin-left:20px;'>No hi ha empleats</p>";
        }
        exit;
    }

}

